//
//  UserRecommendCell.m
//  danpin
//
//  Created by chuangjia on 29/8/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "UserRecommendCell.h"
#import "ZUIImageView.h"

@interface UserRecommendCell ()
{
    
}
@property(nonatomic,strong)UIImageView *imagesView;
@property(nonatomic,strong)RTLabel *titleLabel;
@property(nonatomic,strong)RTLabel *brandLabel;

@property(nonatomic,strong)RTLabel *introLabel;
@property(nonatomic,strong)RTLabel *priceLabel;
@property(nonatomic,strong)UILabel *picLabel;
@property(nonatomic,strong)UIImageView *shaidanView;
@property(nonatomic,strong)UIImageView *lineView;
@end
@implementation UserRecommendCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor whiteColor];
        _imagesView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _imagesView.backgroundColor = UIColorFromRGB(0xffffff);
        _imagesView.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_imagesView];
        
        _brandLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*3-IMAGESIZE,0)];
        _brandLabel.backgroundColor = [UIColor clearColor];
        _brandLabel.textColor = UIColorFromRGB(0x891404);
        _brandLabel.textAlignment = NSTextAlignmentLeft;
        _brandLabel.font = [UIFont boldSystemFontOfSize:14];
        _brandLabel.lineSpacing =4;
        [self.contentView addSubview:_brandLabel];
        
        _titleLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*3-IMAGESIZE,0)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = [UIColor darkGrayColor];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _titleLabel.lineSpacing =4;
        [self.contentView addSubview:_titleLabel];
        
        _introLabel=[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*3-IMAGESIZE,0)];
        _introLabel.backgroundColor = [UIColor clearColor];
        _introLabel.textColor = UIColorFromRGB(0xa5a5a5);
        _introLabel.textAlignment = NSTextAlignmentLeft;
        _introLabel.font = FONT(14);
        _introLabel.lineSpacing =4;
        [self.contentView addSubview:_introLabel];
        
        UITapGestureRecognizer* singleRecognizer8= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom4)];
        _priceLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*3-IMAGESIZE,0)];
        _priceLabel.backgroundColor = [UIColor clearColor];
        _priceLabel.textColor = [UIColor lightGrayColor];
        _priceLabel.textAlignment = NSTextAlignmentLeft;
        _priceLabel.font = [UIFont boldSystemFontOfSize:14];
        _priceLabel.lineSpacing =4;
        _priceLabel.userInteractionEnabled =YES;
        [_priceLabel addGestureRecognizer:singleRecognizer8];
        [self.contentView addSubview:_priceLabel];
        singleRecognizer8=nil;
    
        //
        _picLabel =[[UILabel alloc] initWithFrame:CGRectZero];
        _picLabel.backgroundColor = [UIColor clearColor];
        _picLabel.textColor = [UIColor lightGrayColor];
        _picLabel.textAlignment = NSTextAlignmentRight;
        _picLabel.font = FONT(12);
        [self.contentView addSubview:_picLabel];
        //
        _shaidanView =[[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_shaidanView];
        //
        _lineView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _lineView.image =[UIImage imageNamed:@"line_xu.png"];
        [self.contentView addSubview:_lineView];
    }
    return self;
}
-(void)dealloc
{
    self.data=nil;
    self.actionBlock =nil;
    self.imagesView.image=nil;
    [self.imagesView removeFromSuperview];
    self.imagesView=nil;
    [self.titleLabel removeFromSuperview];
    self.titleLabel=nil;
    [self.brandLabel removeFromSuperview];
    self.brandLabel=nil;
    [self.introLabel removeFromSuperview];
    self.introLabel=nil;
    [self.priceLabel removeFromSuperview];
    self.priceLabel=nil;
    [self.picLabel removeFromSuperview];
    self.picLabel =nil;
    [self.shaidanView removeFromSuperview];
    self.shaidanView=nil;
    [self.lineView removeFromSuperview];
    self.lineView=nil;
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    
    id asking =[self.data objectForKey:@"asking"];
    if (asking && [asking isKindOfClass:[NSDictionary class]]) {
        NSString *askt = [[NSString stringWithFormat:@"%@", [asking objectForKey:@"title"]] replaceNullString];
        if (askt.length>0) {
            askt = [[NSString stringWithFormat:@"已推荐到： %@", askt] replaceNullString];
            [_priceLabel setText:askt];
            CGSize optimumSize = [_priceLabel optimumSize];
            CGFloat h2 =optimumSize.height;
            _priceLabel.frame=CGRectMake(IMAGESIZE+PHOTO_FRAME_WIDTH*2, self.bounds.size.height-h2-PHOTO_FRAME_WIDTH, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*3-IMAGESIZE, h2);
        }
    }
    
    
    self.lineView.frame =CGRectMake(0, self.bounds.size.height-1, DEVICE_WIDTH, 1);
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.indexPath=indexPath;
    self.key = key;
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    CGFloat fTop =PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING;
    NSString * strURL = [NSString stringWithFormat:@"%@", [dict objectForKey:@"largePicURL"]];
    CGFloat H =0;
    if (strURL && strURL.length>0) {
        BOOL need = false;
        CGFloat w =[[dict objectForKey:@"largePicWidth"] floatValue];
        CGFloat h =[[dict objectForKey:@"largePicHeight"] floatValue];
        if (w>0 && h>0) {
            CGFloat fullW =IMAGESIZE;
            CGFloat ratio = fullW / w;
            H = ratio * h;
        }
        else
        {
            H = IMAGESIZE;
            
            need = YES;
        }
        _imagesView.frame =CGRectMake(PHOTO_FRAME_WIDTH,fTop, IMAGESIZE, H);
        __block UIActivityIndicatorView *activityIndicator=nil;
        __weak UIImageView *weakImageView = _imagesView;
        
        [weakImageView sd_setImageWithURL:[NSURL URLWithString:strURL] placeholderImage:nil options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            if (!activityIndicator)
            {
                [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
                activityIndicator.backgroundColor=[UIColor clearColor];
                activityIndicator.center = weakImageView.center;
                [activityIndicator startAnimating];
            }
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (error) {
                image=[UIImage imageNamed:@"listcellimage.png"];
                weakImageView.image=[UIImage imageNamed:@"listcellimage.png"];
            }
            
            CGFloat fImageSize = IMAGESIZE - PHOTO_FRAME_WIDTH;
            CGFloat w =image.size.width;
            CGFloat h =image.size.height;
            CGFloat ratio = fImageSize / w;
            CGFloat x=0;
            if(ratio <= 1)
            {
                w =fImageSize;
                h = h*ratio;
            }
            if(need || ratio > 1)
            {
                if(w > h)
                {
                    
                    CGFloat r = h / w;
                    if(w > fImageSize)
                        w = fImageSize;
                    
                    h = w * r;
                }
                else
                {
                    CGFloat r = w / h;
                    if(h > fImageSize)
                        h = fImageSize;
                    
                    w = h * r;
                }
                x = (fImageSize-w)/2;
            }
            if (h > DEVICE_WIDTH/2) {
                h =  DEVICE_WIDTH/2;
            }
            weakImageView.frame = CGRectMake( PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING +x,fTop, w, h);
            int is_shaidan =[[dict objectForKey:@"is_shaidan"] intValue];
            if (is_shaidan>0)
            {
                _shaidanView.frame =CGRectMake(PHOTO_FRAME_WIDTH+IMAGESIZE-32-PHOTO_FRAME_WIDTH,weakImageView.frame.origin.y+weakImageView.frame.size.height- 16 - PHOTO_TIME_PADDING, 32, 16);
                NSString *siconView =[NSString stringWithFormat:@"%@",[dict objectForKey:@"icon_url"]];
                [_shaidanView sd_setImageWithURL:[NSURL URLWithString:siconView]];
                
            }
            [weakImageView.layer addAnimation:[CATransition animation] forKey:kCATransition];
            
            [activityIndicator removeFromSuperview];
            activityIndicator = nil;
        }];
        
    }
    fTop=_imagesView.frame.origin.y;
    NSString *brand =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"brand"]] replaceNullString];
    if (brand.length>0) {
        [_brandLabel setText:brand];
        CGSize optimumSize11 = [_brandLabel optimumSize];
        CGFloat hh1 =optimumSize11.height;
        _brandLabel.frame =CGRectMake(IMAGESIZE+PHOTO_FRAME_WIDTH*2, fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*3-IMAGESIZE, hh1);
        fTop = fTop+hh1+PHOTO_TIME_PADDING;
    }
    
    NSString *test =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
    if(test.length>0)
    {
        [_titleLabel setText:test];
        CGSize optimumSize = [_titleLabel optimumSize];
        CGFloat h1 =optimumSize.height;
        _titleLabel.frame =CGRectMake(IMAGESIZE+PHOTO_FRAME_WIDTH*2, fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*3-IMAGESIZE, h1);
        fTop += h1+PHOTO_FRAME_WIDTH;
    }
   
    //
    NSString *text =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]] replaceNullString];
    if (text.length>0) {
        text=[NSString stringWithFormat:@"<font size=17 color='#CCCCCC'><b>“ </b></font>%@<font size=17 color='#CCCCCC'><b> ”</b> </font>",text];
        [_introLabel setText:text];
        CGSize optimumSize1 = [_introLabel optimumSize];
        CGFloat h =optimumSize1.height;
        
        _introLabel.frame =CGRectMake(IMAGESIZE+PHOTO_FRAME_WIDTH*2, fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*3-IMAGESIZE, h);
//        fTop += h+PHOTO_FRAME_WIDTH*5;
    }
    //
    
    //
    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)likeAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_like,sender,self.data,self.key,self.indexPath);
    }
}
-(void)commentAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_comment,sender,self.data,self.key,self.indexPath);
    }
}
-(void)categoryAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_category,sender,self.data,self.key,self.indexPath);
    }
}
-(void)handleSingleTapFrom4
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_qiugou,nil,self.data,self.key,self.indexPath);
    }
}
-(void)handleSingleTapFrom
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_good,nil,self.data,self.key,self.indexPath);
    }
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary*)model;
    
    CGFloat w =[[dict objectForKey:@"largePicWidth"] floatValue];
    CGFloat h =[[dict objectForKey:@"largePicHeight"] floatValue];
    CGFloat H =0;
    if (w>0 && h>0) {
        CGFloat fullW =IMAGESIZE;
        CGFloat ratio = fullW / w;
        H = ratio * h;
    }else{
        H=IMAGESIZE;
    }
    if (H > DEVICE_WIDTH/2) {
        H =  DEVICE_WIDTH/2;
    }
    H += 16 + PHOTO_TIME_PADDING*2 + (PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING)*2;
    
    CGFloat hight =PHOTO_FRAME_WIDTH ;
    NSString *brand =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"brand"]] replaceNullString];
    if (brand.length>0) {
        RTLabel *text1Label =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*3-IMAGESIZE,1)];
        text1Label.font = [UIFont boldSystemFontOfSize:14];
        text1Label.lineSpacing = 4;
        text1Label.textAlignment = NSTextAlignmentLeft;
        [text1Label setText:[NSString stringWithFormat:@"%@",brand]];
        CGSize optimumSize = [text1Label optimumSize];
        text1Label=nil;
        CGFloat h1 =optimumSize.height;
        hight += h1+PHOTO_TIME_PADDING;
    }
    
    NSString *skuname =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
    
    if (skuname.length>0) {
        RTLabel *text1Label =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*3-IMAGESIZE,1)];
        text1Label.font = [UIFont boldSystemFontOfSize:14];
        text1Label.lineSpacing = 4;
        text1Label.textAlignment = NSTextAlignmentLeft;
        [text1Label setText:[NSString stringWithFormat:@"%@",skuname]];
        CGSize optimumSize = [text1Label optimumSize];
        text1Label=nil;
        CGFloat h1 =optimumSize.height;
        
        hight += h1+PHOTO_FRAME_WIDTH;
    }
    
    NSString *text =[[NSString stringWithFormat:@"%@", [model objectForKey:@"text"]] replaceNullString];
    if (text.length>0) {
        text =[NSString stringWithFormat:@"<font size=17 color='#CCCCCC'><b>“ </b></font>%@<font size=17 color='#CCCCCC'><b> ” </b> </font>",text];
        RTLabel *textLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*3-IMAGESIZE,1)];
        textLabel.font = FONT(14);
        textLabel.lineSpacing = 4;
        textLabel.textAlignment = NSTextAlignmentLeft;
        [textLabel setText:text];
        CGSize optimumSize1 = [textLabel optimumSize];
        textLabel=nil;
        hight +=optimumSize1.height+PHOTO_FRAME_WIDTH*5;
    }
    id asking =[dict objectForKey:@"asking"];
    if (asking && [asking isKindOfClass:[NSDictionary class]]) {
        NSString *askt = [[NSString stringWithFormat:@"%@", [asking objectForKey:@"title"]] replaceNullString];
        if (askt.length>0) {
            askt = [[NSString stringWithFormat:@"已推荐到： %@", askt] replaceNullString];
            RTLabel *text1Label =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*3-IMAGESIZE,1)];
            text1Label.font = [UIFont boldSystemFontOfSize:14];
            text1Label.lineSpacing = 4;
            text1Label.textAlignment = NSTextAlignmentLeft;
            [text1Label setText:askt];
            CGSize optimumSize = [text1Label optimumSize];
            text1Label=nil;
            CGFloat h1 =optimumSize.height;
            hight += h1+PHOTO_FRAME_WIDTH;
        }
    }
    
    //  CGFloat hh = H ;
    if (hight<H) {
        return H;
    }
    return hight;
}

@end
